/**
 * 
 * @authors cherish yii2 (cherish@cherish.pw)
 * @date    2021-07-28 12:48:28
 * @version v2.0
 * @description the core js of todolist project
 * 
 * ━━━━━━神兽出没━━━━━━
 * 　　   ┏┓　 ┏┓
 * 　┏━━━━┛┻━━━┛┻━━━┓
 * 　┃              ┃
 * 　┃       ━　    ┃
 * 　┃　  ┳┛ 　┗┳   ┃
 * 　┃              ┃
 * 　┃       ┻　    ┃
 * 　┃              ┃
 * 　┗━━━┓      ┏━━━┛ Code is far away from bugs with the animal protecting.
 *       ┃      ┃     神兽保佑,代码无bug。
 *       ┃      ┃
 *       ┃      ┗━━━┓
 *       ┃      　　┣┓
 *       ┃      　　┏┛
 *       ┗━┓┓┏━━┳┓┏━┛
 *     　  ┃┫┫　┃┫┫
 *     　  ┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

// 请根据考试说明文档中列出的需求进行作答
// 预祝各位顺利通过本次考试！
// ...

/* 
 本地持久化存储数据
*/
if(!JSON.parse(localStorage.getItem('todo'))){
    localStorage.setItem('todo',JSON.stringify(['打豆豆','睡觉','吃饭']))
}
if(!JSON.parse(localStorage.getItem('done'))){
    localStorage.setItem('done',JSON.stringify(['打豆豆','睡觉','吃饭']))
}
let todo = JSON.parse(localStorage.getItem('todo'))
let done = JSON.parse(localStorage.getItem('done'))
function render(todo,done){
    let todotext = ''
    let donetext = ''
    todo.forEach((item,index)=>{
        todotext += `
        <li>
            <input type="checkbox" data-id="${index}"/>
            <p onclick="edit(${index + 1})">${item}</p>
            <a href="javascript:remove(${index + 1})" data-id="${index}">-</a>
        </li>
    `
    })
    $('#todolist').html(todotext)
    done.forEach((item,index)=>{
        donetext += `
        <li>
            <input type="checkbox" checked="checked" data-id="${index}"/>
            <p onclick="edit(${index + 1})">${item}</p>
            <a href="javascript:remove(${index + 1})" data-id="${index}">-</a>
        </li>
    `
    })
    $('#donelist').html(donetext)
    $('#todocount').text($('#todolist>li').length)
    $('#donecount').text($('#donelist>li').length)
}
render(todo,done)
/**
 * 添加代办事项
 */
function add(){
    $('#title').focus(function(){
        $('#title').keydown(function(e){
            e.preventDefault()
            if(e.keyCode == 13){
                const text = $('#title').val().trim()
                if(!text) return
                let todo = JSON.parse(localStorage.getItem('todo'))
                todo = [...todo,text]
                localStorage.setItem('todo',JSON.stringify(todo))
                render(todo,done)
                $('#title').val('')
            }
        })
        
    })
}
add()
/**
 * 编辑代办事项
 */
function edit(){
    /* 完成和未完成状态的切换 */
    $('.event').on('click','input',function(){
        let todo = JSON.parse(localStorage.getItem('todo'))
        let done = JSON.parse(localStorage.getItem('done'))
        if($(this).prop('checked')){
            done.push(todo[$(this).attr('data-id')])
            todo.splice($(this).attr('data-id'),1)
            localStorage.setItem('todo',JSON.stringify(todo))
            localStorage.setItem('done',JSON.stringify(done))
            render(todo,done)
        }else{
            todo.push(done[$(this).attr('data-id')])
            done.splice($(this).attr('data-id'),1)
            localStorage.setItem('todo',JSON.stringify(todo))
            localStorage.setItem('done',JSON.stringify(done))
            render(todo,done)
        }
    })
    $('li>p').dblclick(function(e){
        e.stopPropagation()
        const m =prompt("请输入修改后的内容")
        if(m){
            $(this).text(m.trim())
        }
    })
}
edit()
/**
 * 移除代办事项
 */
function remove(){
    $('#todolist').on('click','a',function(){
        let todo = JSON.parse(localStorage.getItem('todo'))
        let done = JSON.parse(localStorage.getItem('done'))
        const del = $(this).prev().text()
        if($.inArray(del,todo) == -1) return
        todo.splice($.inArray(del,todo),1)
        localStorage.setItem('todo',JSON.stringify(todo))
        render(todo,done)
    })
    $('#donelist').on('click','a',function(){
        let todo = JSON.parse(localStorage.getItem('todo'))
        let done = JSON.parse(localStorage.getItem('done'))
        const del = $(this).prev().text()
        if($.inArray(del,done) == -1) return
        done.splice($.inArray(del,done),1)
        localStorage.setItem('done',JSON.stringify(done))
        render(todo,done)
    })
}
remove()
/**
 * 跨域解决方案代码/配置粘贴处（请粘贴在块状注释范围内）

location /jstest {
            proxy_pass https://api.i-lynn.cn/getIpInfo;
        }
 */
$.ajax({
    url:'/jstest',
    success:function(res){
        let foot = `
        <p>
            Copyright &copy; 2021 千锋教育 版权所有
            <a href="javascript:;">京ICP备15058198号</a>
        </p>
        <p>
            您当前的ip地址是：<strong id="ipv4">${res.ip}</strong
            >，地理位置：<strong id="addr">${res.area}</strong>
        </p>
        `
        
        $('footer').html(foot)
    },
    error:function(err){
        console.log(err)
    }
})